/**
 * 我的纸条业务逻辑代码
 * */

const db = require("../../db/db")
const initModel = require("../../models/init-models")
const {scrip, middle, scrip_img} = initModel(db);
const {Op} = require("sequelize");
// 引入删除七牛云图片
let delQiniu = require("../../comment/delQiniu");

// 纸条和纸条图片表关联
scrip.hasMany(scrip_img, {
  targetKey: "id",
  foreignKey: "scrip_id"
})
scrip_img.belongsTo(scrip, {
  targetKey: "id",
  foreignKey: "scrip_id"
})

// 使用中间表过渡查取抽取的纸条数据
scrip.hasOne(middle, {
  foreignKey: 'scrip_id',
});
middle.belongsTo(scrip, {
  foreignKey: 'scrip_id',
});

module.exports = {
  // 获取纸条
  getScrip: async (req, res) => {
    // 获取参数
    let {page, limit, scrip_type} = req.body;
    // 判断传参是否有效
    if (scrip_type != 1 && scrip_type != 2) {
      res.send({
        status: 400,
        msg: "传值有误"
      });
      return;
    }
    // 页码和条数
    page = page == null ? 1 : Number(page);
    limit = limit == null ? 5 : Number(limit);
    // 跨越条数
    let offset = (page - 1) * limit;
    // 获取用户id
    let {id} = req.user;

    // 判断获取哪种纸条
    let getScrip = null;
    if (scrip_type == 1) {
      // 放入的
      getScrip = await scrip.findAll({
        where: {user_id: id},
        include: [
          {
            model: scrip_img,
            attributes: {exclude: ["scrip_id"]}
          }
        ],
      })
    } else if (scrip_type == 2) {
      // 抽取的
      let take = await middle.findAll({
        where: {user_id: id},
        attributes: [],
        include: [
          {
            model: scrip,
            include: [
              {
                model: scrip_img,
                attributes: {exclude: ["scrip_id"]}
              }
            ]
          }
        ],
      });
      // getScrip.count = count;
      // 遍历筛选数据
      getScrip = take.map(item => {
        return item.scrip
      })
    }
    // 返回数据
    res.send({
      status: 200,
      msg: "获取成功",
      data: {
        // count: getScrip.count,
        scrip: getScrip
      }
    })

  },
  // 删除用户抽取或放入的纸条
  delScrip: async (req, res) => {
    // 获取参数
    let {scrip_id, type} = req.body;
    // 判断是否传值
    if (scrip_id == null || type != 1 && type != 2) {
      res.send({
        status: 400,
        msg: "缺少参数"
      });
      return;
    }
    // 获取用户id
    let {id} = req.user;
    // 判断删除哪个
    let findScrip = null;
    if (type == 1) {
      // 放入
      findScrip = await scrip.findOne({
        where: {
          user_id: id,
          id: scrip_id
        }
      })
    } else if (type == 2) {
      // 抽取
      let findId = await middle.findOne({
        where: {
          user_id: id,
          scrip_id
        }
      })
      if (findId != null) {
        // 证明有抽取纸条
        findScrip = await scrip.findOne({where: {id: findId.scrip_id}});
        // 删除中间表数据
        await findId.destroy();
      }
    }
    // 判断查找的图片是否为空
    if (findScrip != null) {
      // 获取图片数据
      let findScripImg = await scrip_img.findAll({
        where: {scrip_id: findScrip.id},
        attributes: ["img"]
      })

      if (findScripImg != null) {
        // 处理图片  循环删除七牛云图片
        for (const i of findScripImg) {
          i.img = i.img.split("com/")[1];
          await delQiniu(i.img)
        }
        // 删除纸条图片
        await scrip_img.destroy({
          where: {scrip_id: findScrip.id},
        });
      }

      // 删除纸条数据
      await findScrip.destroy();
      // 返回
      res.send({
        status: 200,
        msg: "删除成功"
      })
    } else {
      res.send({
        status: 400,
        msg: "没有该纸条"
      })
    }


  }
}